package com.kkui.liuxin.controller;

import com.alibaba.fastjson.JSONObject;
import com.kkui.liuxin.service.MkdirService;
import com.kkui.liuxin.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;

/**
 * @ClassName: FileMove
 * @Description: file and folder move
 * @author: kkui
 * @date: 2022/4/18 18:29
 * @status:516
 */

@Slf4j
@RequestMapping("/api")
@Controller
public class FileMove {
    @Resource
    MkdirService mkdirService;
    @Resource
    UserService userService;

    @ResponseBody
    @PostMapping("/move")
    public String move(@RequestBody String jsonString, @RequestHeader("Authorization") String authorization, HttpServletResponse response){
        JSONObject jsonObject = JSONObject.parseObject(jsonString);

        String oldPath = jsonObject.getString("oldPath");
        String newPath = jsonObject.getString("newPath");

        File file1 = new File(oldPath);
        File file = new File(newPath);
        log.info(file.getParent() + ' ' + file1.getPath());
        if (file.getParent().contains(file1.getPath())){
            response.setStatus(516);
            return "不能移动到自己及子文件夹下";
        }

        int userId = userService.jwtGetUserId(authorization);

        mkdirService.move(userId + oldPath.replace('/', File.separatorChar),userId + newPath.replace('/', File.separatorChar));

        return "移动成功";
    }
}
